package com.xuebank.nfe.nfecar.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ProgressBar
import android.widget.RelativeLayout
import com.madai.eazyloan.globals.PERMISSION_LAUCH
import com.xuebank.nfe.nfecar.MainActivity
import com.xuebank.nfe.nfecar.R
import com.xuebank.nfe.nfecar.beas.BaseActivity
import com.xuebank.nfe.nfecar.globals.Constants
import com.xuebank.nfe.nfecar.utils.AlertModel
import com.xuebank.nfe.nfecar.utils.IntentUtil
import com.xuebank.nfe.nfecar.utils.PermissionUtil
import java.util.*

class WelcomeActivity : BaseActivity() {

    private var rl_launch:RelativeLayout? = null
    private var timer: Timer? = null
    private var timeNum = 5
    private var pbPercent = 100
    private var pbLaunch: ProgressBar? = null

    internal var task: TimerTask = object : TimerTask() {
        override fun run() {
            runOnUiThread {
                timeNum--
                pbPercent -= 20
                pbLaunch?.setProgress(pbPercent)
                if (timeNum < 0) {
                    timer?.cancel()
                    rl_launch?.visibility = View.GONE
                    pbLaunch?.progress = 0
                    this@WelcomeActivity.finish()
                    goToActivity(MainActivity::class.java)
                }
            }
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_welcome)

    }

    override fun findViews() {
        super.findViews()
        rl_launch = findViewById(R.id.rl_launch)
        pbLaunch = findViewById(R.id.pb_launch)
    }

    override fun initViews() {
        super.initViews()
        rl_launch?.setOnClickListener(this@WelcomeActivity)
        timer = Timer()
        timer?.schedule(task, 1000, 1000)
        setProgressBarIndeterminateVisibility(true)
        pbLaunch?.progress = 100
    }

    override fun onViewClick(v: View) {
        super.onViewClick(v)
        when (v.id) {
            R.id.rl_launch ->{
                this@WelcomeActivity.finish()
                goToActivity(MainActivity::class.java)
            }
        }
    }






    private fun goToActivity(goClass: Class<out Activity>, extra: Bundle? = null) {
        val intent = intent
        intent.setClass(this, goClass)
        intent.type = Constants.ACTION_DO_FIRST_INIT
        if (goClass == MainActivity::class.java) {
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        }
        if (null != extra)
            intent.putExtras(extra)
        startActivity(intent)
        try {
            finish()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}
